package jp.inaka.mironal.android.tad.content.provider;

import java.util.HashMap;

import jp.inaka.mironal.android.tad.content.ContentOpenHelper;
import jp.inaka.mironal.android.tad.content.table.TaskTable;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteConstraintException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;

public class TaskProvider extends ContentProvider{

    private static final int TASK = 1;

    private static final int TASK_ID = 2;

    private static UriMatcher URI_MATCHER;

    private static HashMap<String, String> sTaskProjectionMap;

    static{
        URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
        URI_MATCHER.addURI(TaskTable.AUTHORITY, TaskTable.PATH, TASK);
        URI_MATCHER.addURI(TaskTable.AUTHORITY, TaskTable.PATH + "/#", TASK_ID);

        sTaskProjectionMap = new HashMap<String, String>();
        sTaskProjectionMap.put(TaskTable._ID, TaskTable._ID);
        sTaskProjectionMap.put(TaskTable.NAME, TaskTable.NAME);
    }

    private ContentOpenHelper mOpenHelper;

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        // TODO �����������ꂽ���\�b�h�E�X�^�u
        return 0;
    }

    @Override
    public String getType(Uri uri) {
        // TODO �����������ꂽ���\�b�h�E�X�^�u
        return null;
    }

    @Override
    public Uri insert(Uri uri, ContentValues initialValues) {
        SQLiteDatabase db = mOpenHelper.getWritableDatabase();

        int match = URI_MATCHER.match(uri);
        if( match == TASK ){
            ContentValues values;
            if( initialValues == null ){
                values = new ContentValues();
            }else{
                values = new ContentValues(initialValues);
            }

            if( values.containsKey(TaskTable.NAME) == false ){
                values.put(TaskTable.NAME, "no name.");
            }

            long rowId;
            try {
                rowId = db.insertOrThrow(ContentOpenHelper.TASK_TABLE, null, values);
                if( rowId > 0 ){
                    Uri taskUri = ContentUris.withAppendedId(TaskTable.CONTENT_URI, rowId);
                    getContext().getContentResolver().notifyChange(taskUri, null);
                    return taskUri;
                }else{
                    throw new SQLException( "Failed to insert row into " + uri );
                }
            }catch (SQLiteConstraintException e) {
                return null;

            }
        }else{
            throw new IllegalArgumentException();
        }
    }

    @Override
    public boolean onCreate() {
        mOpenHelper = new ContentOpenHelper(getContext());
        return true;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
            String sortOrder) {
        SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
        qb.setTables(ContentOpenHelper.TASK_TABLE);
        qb.setProjectionMap(sTaskProjectionMap);

        switch (URI_MATCHER.match(uri)) {
            case TASK:

                break;

            case TASK_ID:
                qb.appendWhere(TaskTable._ID
                            + " = "
                            + uri.getPathSegments().get(1) );

                break;
            default:
                throw new IllegalArgumentException();
        }

        SQLiteDatabase db = mOpenHelper.getReadableDatabase();
        Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, sortOrder);
        c.setNotificationUri(getContext().getContentResolver(), uri);
        return c;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        // TODO �����������ꂽ���\�b�h�E�X�^�u
        return 0;
    }

}
